package com.zang.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @author Zhang Qiang
 * @Date 2019/9/2 16:59
 */
public class SendMallUtil {

    /**
     * 密钥
     */
    private static final String ACCESS_KEY_ID = "LTAI0fYTaUmBaj3W";
    private static final String ACCESS_KEY_SECRET = "EG99bd9O9991g9pu999o5ne999Yvg99iFk9pPetVM999i0FleVCY1999992";

    /**
     * 控制台创建的发信地址
     */
    private static final String ACCOUNT_NAME = "admin@zysai.com";
    /**
     * 发信人昵称
     */
    private static final String FROM_ALIAS = "zysai.com";
    /**
     * 0 为随机账号；1 为发信地址。
     */
    private static final Integer ADDRESS_TYPE = 1;
    /**
     * 标签
     */
    private static final String TAG_NAME = "zySai";

    /**
     * 多个收件人发送邮件，收件人之间用逗号分开
     * request.setToAddress("邮箱1,邮箱2");
     * @param toAddress 发送地址
     * @param theMe 邮件主题
     * @param content 内容
     * @return RequestId
     */
    public static String sendMail(String toAddress,String theMe, String content){
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        SingleSendMailRequest request = new SingleSendMailRequest();
        SingleSendMailResponse httpResponse = null;
        try {
            request.setAccountName(ACCOUNT_NAME);
            request.setFromAlias(FROM_ALIAS);
            request.setAddressType(ADDRESS_TYPE);
            request.setTagName(TAG_NAME);
            request.setReplyToAddress(true);
            request.setToAddress(toAddress);
            //邮件主题
            request.setSubject(theMe);
            //邮件正文
            request.setHtmlBody(content);
            httpResponse = client.getAcsResponse(request);
        } catch (ClientException e) {
            //捕获错误异常码
            System.out.println("ErrCode : " + e.getErrCode());
            e.printStackTrace();
        }

        return httpResponse == null ? "" : httpResponse.getRequestId();

    }

}
